/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Produccion;

import Controlador.Bean.Produccion.ProduccionReal;
import Controlador.Service.Produccion.ServicioAsignacionReal;
import gui.Produccion.agregarProduccion;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.swing.JComboBox;
import javax.swing.table.AbstractTableModel;
import lib.Validaciones;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author César
 */
public class TablaAsignacionReal extends javax.swing.JPanel {

    /**
     * Creates new form TablaAsignacionReal
     */
    int editable = 0;
    JXDatePicker jdpRegistro;
    JComboBox cmbTurno;
    ServicioAsignacionReal ar;
    TableModel tb;
    int flag = 0;
    List<List<HashMap<String,Object>>> productos;
    String tma;
    
    public void desabilitarCampos(){
        editable = 1;
        tb.fireTableStructureChanged();
    }
    
    public TablaAsignacionReal(JXDatePicker jdpRegistro, JComboBox cmbTurno , String tma) {
        ar = new ServicioAsignacionReal();
        this.tma = tma;
        this.jdpRegistro = jdpRegistro;
        this.cmbTurno = cmbTurno;
        initComponents();
        tb = new TableModel();
        tblAsignacion.setModel(tb);
        
        //---------------------------------------

        productos = new ArrayList<>();
        for(int i=0; i<tb.data.size(); i++){
            productos.add(ar.obtenerIngredientes(tb.data.get(i).getIdProducto()));
        }
        flag = 1;
        //---------
    }

    public void actualizarTabla(){
        tb.actualizarTabla();
    }
    
    class TableModel extends AbstractTableModel{
        
        //Two arrays used for the table data
        String[] columnNames = {"Operador", "Fecha", "Producto" , "Turno", "Cantidad Estimada", "Cantidad Real", "MermaFicticia", "MermaReal"};
               
        public List<ProduccionReal> data = ar.obtenerServicioAsignacionReal(new Date(), "TUR000001", tma);
        
        
        public void actualizarTabla(){
            data = ar.obtenerServicioAsignacionReal(jdpRegistro.getDate()!=null?jdpRegistro.getDate():new Date(), cmbTurno.getSelectedItem()=="Día"?"TUR000001":"TUR000002" ,tma);
            fireTableDataChanged();
        }
        
        @Override
        public int getRowCount()
        {
            return data.size();
        }
        
        @Override
        public void setValueAt(Object o, int row, int column){
            if(Validaciones.validarRealMg(o.toString())){
                if(column == 5)
                    tb.data.get(row).setCantidadReal(Double.parseDouble(o.toString()));
                else
                    tb.data.get(row).setMermaReal(Double.parseDouble(o.toString()));
                fireTableDataChanged();
            }
        }
        
        @Override
        public int getColumnCount()            
        {
            return columnNames.length;
        }
        
        @Override
        public Object getValueAt(int row, int column)
        {   
            switch(column){
                case 0: return data.get(row).getNombreOperador();
                case 1: return data.get(row).getFecha(); 
                case 2: return data.get(row).getNombreProducto();
                case 3: return data.get(row).getNombreTurno();
                case 4: return data.get(row).getCantidadFicticia();
                case 5: return data.get(row).getCantidadReal();
                case 6: return data.get(row).getMermaFicticia();
                default: return data.get(row).getMermaReal();                    
            }
        }
        
        //Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }
        
        //Used by the JTable object to render different
        //functionality based on the data type
        @Override
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }
        
        @Override
        public boolean isCellEditable(int row, int column)
        {
            if(editable==0){
                if(column == 5 || column == 7)
                    return true;
                return false;
            }else{
                return false;
            }
        }
        
     }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblAsignacion = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(462, 140));
        setMinimumSize(new java.awt.Dimension(462, 140));
        setPreferredSize(new java.awt.Dimension(462, 140));
        setLayout(new java.awt.BorderLayout());

        tblAsignacion.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblAsignacion);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblAsignacion;
    // End of variables declaration//GEN-END:variables
}
